package miage.rest;

import java.util.HashMap;
import java.util.Map;
import miage.rest.resource.candidature.CandidatResource;
import miage.rest.resource.candidature.CandidatureResource;
import miage.rest.resource.candidature.CandidaturesResource;
import miage.rest.resource.candidature.StatutResource;
import miage.rest.resource.candidature.StatutsResource;
import miage.rest.resource.misc.StatsResource;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.representation.StringRepresentation;
import org.restlet.routing.Router;
import org.restlet.routing.TemplateRoute;
import org.restlet.routing.Variable;

/**
 *
 * @author smith
 */
public class Application extends org.restlet.Application {

    private final Router router;

    public Application(Router router) {
        this.router = router;
    }

    @Override
    public synchronized Restlet createInboundRoot() {
        router.setContext(getContext());

		router.attach("/stats", StatsResource.class);
        router.attach("/candidats/{idCandidat}", CandidatResource.class);
        router.attach("/candidatures/{idCandidature}", CandidatureResource.class);
        router.attach("/sessions/{idSession}/candidatures", CandidaturesResource.class);
        router.attach("/candidature/statuts", StatutsResource.class);
        router.attach("/candidatures/{idCandidature}/statut", StatutResource.class);

		for(TemplateRoute route : router.getRoutes()) {
			route.getTemplate().setVariables(getVariables());
		}
        /**
         * Consultation de la liste des candidatures pour une session
         * GET /sessions/{idSession}/candidatures?limit=
         */

        /**
         * Premiere candidature pour une session
         * POST /sessions/{idSession}/candidatures
         */
        
        /**
         * Informations concernant les candidatures d'une session
         * HEAD /sessions/{idSession}/candidatures
         */

        /**
         * Emettre une candidature pour une session
         * POST /sessions/{idSession}/candidats/{idCandidat}/candidatures
         */

        /**
         * Modification du statut d'une candidature par un administrateur
         * PUT /candidatures/{idCandidature}/statut
         */

        /**
         * Rappel du mot de passe
         * POST /candidats/{idCandidat}/rappels
         */
        Restlet mainpage = new Restlet() {

            @Override
            public void handle(Request request, Response response) {
                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.append("<html>");
                stringBuilder.append("<head><title>PPD REST</title></head>");
                stringBuilder.append("<body>");
                stringBuilder.append("<h1>Page d'accueil &mdash; PPD REST</h1>");
                stringBuilder.append("</body>");
                stringBuilder.append("</html>");

                response.setEntity(new StringRepresentation(stringBuilder.toString(), MediaType.TEXT_HTML));

            }
        };
        router.attach("/", mainpage);
        return router;
    }

	private Map<String, Variable> getVariables() {
		Map<String, Variable> variables = new HashMap<String, Variable>();

		variables.put("idCandidat", new Variable(Variable.TYPE_DIGIT));
		variables.put("idCandidature", new Variable(Variable.TYPE_DIGIT));

		return variables;
	}
}
